import { reactive, isReactive, ref, toRefs } from "vue";
import { list, listType } from "@/apis/home";
export default function listHook() {
    const info = reactive({
        goodsList: <any>[],
        pageNo: 1,
        pageSize: 4,
        refreshing: false, //下拉
        loading: false, //是否出发滚动事件
        finished: false //没有跟多数据了,
    });
    const requestGoodsList = async () => {
        const { data } = await list({
            pageSize: info.pageSize,
            pageNo: info.pageNo
        });
        info.pageNo += 1;
        info.loading = false;
        if (info.refreshing) {
            info.goodsList = data;
            info.refreshing = false;
            info.finished = false;
        } else {
            info.goodsList = [...info.goodsList, ...data];
        }
        if (data.length < info.pageSize) info.finished = true;
    };

    // 下拉刷新
    const onRefresh = () => {
        if (!info.loading) {
            info.refreshing = true;
            info.pageNo = 1;
            requestGoodsList();
        }
    };

    // 滚动
    const onLoad = () => {
        if (!info.finished) {
            info.loading = true;
            requestGoodsList();
        }
    };

    return { requestGoodsList, ...toRefs(info), onRefresh, onLoad };
}
